<?php
/**
 * Created by PhpStorm.
 * User: Sun Jian
 * Date: 2019/1/10
 * Time: 15:23
 */

namespace ms\controllers;

use common\models\DhAbout;
use common\models\DhBanner;
use common\models\DhGoods;
use common\models\DhGoodsCate;
use common\models\DhProject;
use common\models\DhProjectCate;
use common\models\DhUser;
use yii\web\Controller;
use Yii;

class ApiController extends Controller
{
    public $data;
    public function __construct($id, $module, array $config = [])
    {
        //校验seesion
        $request = Yii::$app->request;
        $this->data = json_decode(file_get_contents("php://input"),true);

        parent::__construct($id, $module, $config);
    }

        public function actionGetBanner(){
            $banner = DhBanner::find()->all();
            $return = [];
            if($banner){
                $return['code']=0;
                foreach ($banner as $k=>$v){
                    $return['data'][$k]['img']=$v->img;
                }
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }
        }

        public function actionGetIndexGoods(){
            $goods = DhGoods::find()->where(['flag'=>1])->offset(0)->limit(3)->all();
            $return = [];
            if($goods){
                $return['code']=0;
                foreach ($goods as $k=>$v){
                    $return['data'][$k]['id']=$v->id;
                    $return['data'][$k]['img']=$v->img;
                    $return['data'][$k]['name']=$v->name;
                    $return['data'][$k]['brand']=$v->brand;
                    $return['data'][$k]['price']=$v->price;
                    $return['data'][$k]['standard']=$v->standard;
                    $return['data'][$k]['stock']=$v->stock;

                    echo json_encode($return);
                    return ;
                }
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }
        }

        public function actionGetIndexProject(){
            $project = DhProject::find()->where(['flag'=>1])->one();
            $return = [];
            if($project){
                $return['code']=0;
                $return['data']['id']=$project->id;
                $return['data']['img']=$project->img;
                $return['data']['content']=$project->content;
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }
        }


        public function actionGetProjectCate(){
            $cate = DhProjectCate::find()->where(['flag'=>1])->asArray()->all();
            $return = [];
            if($cate){
                $return['code']=0;
                $return['data']=$cate;
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }
        }

        public function actionGetProjectByCate(){
            $id =Yii::$app->request->get("id");
            $return = [];
            if($id == 0){
                $project = DhProject::find()->where(['flag'=>1])->all();
                $return['code']=0;
                foreach ($project as $k=>$v){
                    $return['data'][$k]['img']=$v->img;
                    $return['data'][$k]['id']=$v->id;
                    $return['data'][$k]['content']=$v->content;
                    $return['data'][$k]['unit']=$v->unit;
                    $return['data'][$k]['price']=$v->price;
                    $return['data'][$k]['title']=$v->title;
                }
                echo json_encode($return);
                return ;
            }else{
                $project = DhProject::find()->where(['flag'=>1,'c_id'=>$id])->all();
                $return['code']=0;
                foreach ($project as $k=>$v) {
                    $return['data'][$k]['img'] = $v->img;
                    $return['data'][$k]['id'] = $v->id;
                    $return['data'][$k]['content'] = $v->content;
                    $return['data'][$k]['unit'] = $v->unit;
                    $return['data'][$k]['price'] = $v->price;
                }
                    echo json_encode($return);
                return ;
            }
        }

        public function actionGetProjectById(){
            $id =Yii::$app->request->get("id");
            $return = [];
            $project = DhProject::find()->where(['id'=>$id])->one();
            if($project){
                $return['code']=0;
                $return['data']['img']=$project->img;
                $return['data']['id']=$project->id;
                $return['data']['content']=$project->content;
                $return['data']['unit']=$project->unit;
                $return['data']['price']=$project->price;
                $return['data']['title']=$project->title;
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }

        }

        public function actionGetGoodsCate(){
            $cate = DhGoodsCate::find()->where(['flag'=>1])->asArray()->all();
            $return = [];
            if($cate){
                $return['code']=0;
                $return['data']=$cate;
                echo json_encode($return);
                return ;
            }else{
                $return['code']=10000;
                $return['msg']="没有数据";
                echo json_encode($return);
                return ;
            }
        }

    public function actionGetGoodsByCate(){
        $id =Yii::$app->request->get("id");
        $return = [];
        if($id == 0){
            $goods = DhGoods::find()->where(['flag'=>1])->all();
            $return['code']=0;
            foreach ($goods as $k=>$v){
                $return['data'][$k]['id']=$v->id;

                $return['data'][$k]['img']=$v->img;
                $return['data'][$k]['name']=$v->name;
                $return['data'][$k]['brand']=$v->brand;
                $return['data'][$k]['price']=$v->price;
                $return['data'][$k]['standard']=$v->standard;
                $return['data'][$k]['stock']=$v->stock;
            }
            echo json_encode($return);
            return ;
        }else{
            $goods = DhGoods::find()->where(['flag'=>1,'c_id'=>$id])->all();
            $return['code']=0;
            foreach ($goods as $k=>$v){
                $return['data'][$k]['id']=$v->id;
                $return['data'][$k]['img']=$v->img;
                $return['data'][$k]['name']=$v->name;
                $return['data'][$k]['brand']=$v->brand;
                $return['data'][$k]['price']=$v->price;
                $return['data'][$k]['standard']=$v->standard;
                $return['data'][$k]['stock']=$v->stock;
            }
            echo json_encode($return);
            return ;
        }
    }

    public function actionGetGoodsById(){
        $id =Yii::$app->request->get("id");
        $return = [];
        $project = DhGoods::find()->where(['id'=>$id])->one();
        if($project){
            $return['code']=0;
            $return['data']['id']=$project->id;
            $return['data']['img']=$project->img;
            $return['data']['name']=$project->name;
            $return['data']['brand']=$project->brand;
            $return['data']['price']=$project->price;
            $return['data']['standard']=$project->standard;
            $return['data']['stock']=$project->stock;
            echo json_encode($return);
            return ;
        }else{
            $return['code']=10000;
            $return['msg']="没有数据";
            echo json_encode($return);
            return ;
        }
    }


    public function actionGetAbout(){
        $about = DhAbout::find()->where(['id'=>1])->asArray()->one();
        $return['code']=0;
        $return['data']=$about;
        echo json_encode($return);
        return ;
    }

    public function actionLogin(){

        $code = $this->data['code'];
        //var_dump($code);return ;
        $data2 = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=".APPID."&secret=".SECRET."&js_code=".$code."&grant_type=authorization_code");

        $data = json_decode($data2,true);

        $return = [];
        if(!isset($data['errcode'])){
            $openid = $data['openid'];
            $user = DhUser::findOne(['openid'=>$openid]);
            if($user){
                $user->session_code=$data['session_key'];
                $user->save();
                $return['code']=0;
                $return['session']=$data['session_key'];
                $return['openid']=$data['openid'];
                echo json_encode($return);
                return ;
            }else{
                $model = new DhUser();
                $model->session_code=$data['session_key'];
                $model->openid=$data['openid'];
                if($model->save()){
                    $return['code']=0;
                    $return['sessionKey']=$data['session_key'];
                    $return['openid']=$data['openid'];
                    echo json_encode($return);
                    return ;
                }else{
                    $return['code']=110000;
                    $return['msg']=json_encode($model->errors);
                    echo json_encode($return);
                    return ;
                }
            }
        }else{
            $return['code']=110000;
            $return['msg']=$data['errmsg'];
            echo json_encode($return);
            return ;
        }
    }

}